package DBTools;

import java.io.IOException;
import java.util.Properties;

/**
 * this class allow to start and stop the mongodb 
 * @author Tony
 *
 */
public class DBProcess {

	public String windowsStart="mongodb-win32-x86_64-2.0.1/bin/mongod.exe --dbpath db";
	public String linuxStart="mongo";
	
	
	private Process p;	
	
	/**
	 * start the mongo db. If there is there is a mongodb instance on the same port then the programm will not start 
	 */
	public void start(){
		
		String command="";
		
		Properties prop=System.getProperties();
		
		if(prop.getProperty("os.name").toLowerCase().contains("windows")){
                    command=windowsStart;
                } else {
                    command=linuxStart;
                }
		try {
			p = Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * stop the db but only if the db is correctly started with this class
	 */
	public void stop(){	
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		p.destroy();
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DBProcess test = new DBProcess();
		test.start();
		
		
		test.stop();

	}

}
